var xmlHttpRequest;
var itemId2;
function createXMLHttpRequest()
{
    if (window.XMLHttpRequest) //非IE浏览器
    {
        xmlHttpRequest = new XMLHttpRequest();
    }
    else if (window.ActiveObject)//IE6以上版本的IE浏览器
    {
        xmlHttpRequest = new ActiveObject("Msxml2.XMLHTTP");
    }
    else //IE6及以下版本IE浏览器
    {
        xmlHttpRequest = new ActiveObject("Microsoft.XMLHTTP");
    }
}

function updateCart(quantity,itemId) {
     // var quantity = document.getElementById("quantity").value;
    // var quantity = document.getElementsByClassName("quantity_class");
     console.log(quantity);
     console.log(itemId);

    var str = quantity.replace(/(^\s*)|(\s*$)/g, '');//去除空格;
    if (str == '' || str == undefined || str == null) {
        console.log('空')
        confirm("请输入商品数量！");
        // return(0);
    } else {
        console.log('非空');
        var url = "updateCartJSServlet";
        var params = "quantityOfId="+itemId;
        var params2 = "quantity="+quantity;
        sendRequest(url, params, params2);
        itemId2 = itemId;
    }
}

function sendRequest(url,params,params2) {
    createXMLHttpRequest();
    xmlHttpRequest.open("GET", url+ '?' + params + '&' + params2, true);
    xmlHttpRequest.onreadystatechange = processResponse;
    xmlHttpRequest.send(null);

}


function processResponse() {
    // alert(xmlHttpRequest.status);
    // if (xmlHttpRequest.readyState==4 && xmlHttpRequest.status==200){
    // var name = document.getElementById("getRequest");
    // console.log("name "+name.innerText);


            var quantity = document.getElementsByName(""+ itemId2);
            // var total = document.getElementById(""+ itemId);
    var total = document.getElementsByTagName("label");
    var totalOfItem;
    console.log(itemId2);

    for(var i=0; i<total.length; i++){
        console.log(total[i].id.toString());
        if(itemId2.toString() === total[i].id.toString()){
            totalOfItem = total[i];
            break;
        }
    }

            var subtotal = document.getElementById("subtotal");
    var resp = xmlHttpRequest.responseText;
    var array = resp.split(",");
            quantity.innerText = array[0];
            totalOfItem.innerText = array[1];
            subtotal.innerText = array[2];
    // console.log("main total and subtotal "+array[1] + ' '+ array[2]);


}

